package com.wsk.service.Impl;

import com.wsk.bean2.UserInformation;
import com.wsk.dao.LoginDAO;
import com.wsk.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
@Repository
@Transactional
public class LoginServiceImpl implements LoginService{

    @Autowired
    private LoginDAO loginDAO;

    // 保存
    public boolean save(Object t) {
        return loginDAO.save(t);
    }

    // 登录
    public boolean login(int id, String password) {
        String sqlString = "select u.password from " + "userpassword u where u.id='" + id
                + "' and password='" + password + "' limit 1 ";
        return loginDAO.Login(sqlString);
    }

    // 是否为空
    public boolean isNull(String phone) {
        String sql = "select u.phone from " + "userInformation u where u.phone=" + phone+" limit 1 ";
        return loginDAO.isNull(sql);
    }

    // 获取ID
    public int getId(String phone) {
        String sqlString = "select u.id from UserInformation u where u.phone=" + phone+" limit 1 ";
        int id = loginDAO.getId(sqlString);
        return id;
    }

    // 更新密码
    public boolean updatePassword(Object t) {
        return loginDAO.updatePassword(t);
    }

    // 更新头像
    public boolean ImageisEmpty(int id) {
        String sql = "select u.headpicture from " + "UserHeadPicture u where u.id='" + id + "' limit 1 ";
        return loginDAO.ImageisEmpty(sql);
    }

    // 获取图片地址
    public String getPictureURL(int id) {
        String sqlString = "select headpicture from userheadpicture where id=" + id+" limit 1 ";
        return loginDAO.getPictureURL(sqlString);
    }

    // 获取图片地址
    public String getQRCode(int id) {
        String sqlString = "select QRCode from userqrcode where id=" + id+" limit 1 ";
        return loginDAO.getPictureURL(sqlString);
    }
    // 获取用户信息
    public UserInformation getUserInformation(int id) {
        String sql = "select u.id,u.phone,u.address,u.birthday,u.sex,u.autograph,u.label,u.buildtime,u.name,ul.level,ui.integral "
                + " from userinformation as u,userlevel as ul,userintegral as ui where u.id=? and u.id=ul.id and u.id=ui.id limit 1 ";
        return loginDAO.getUserInformation(id,sql);
    }
//    public UserInformation getUserInformation(int id) {
//        return loginDAO.getUserInformation(id);
//    }

    // 单独update
    public boolean update(int id, String method, String value){
        String sql;
        sql = "update userinformation set " + method + " = '" + value + "' where id=" + id;
        return loginDAO.update(sql);
    }

    // 更新生日
    public boolean updateBirthday(int id, Date birthday){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(birthday);
        String sql = "update userinformation set birthday= '" + time + "' where id=" + id;
        return loginDAO.update(sql);
    }

    // 更新等级
    public boolean updateUserLevel(int id, double level){
        String sql = "update userlevel set level=" + level + " where id=" + id;
        return loginDAO.update(sql);
    }

    //更新积分
    public boolean updateIntegral(int id, Object t) {
        String sql = "UPDATE userintegral set integral=integral+1 where id="+id+" ";
        return loginDAO.update(sql) && loginDAO.save(t);
    }
    // 获取积分来源
    public Map<String, String> getIntegralSource(int id) {
        String sql = "select i.id,i.method,i.time,i.iid from integralsource i where i.iid=" + id+" limit 1 ";
        return loginDAO.getIntegralSource(sql);
    }
    // 添加积分来源
    public void addIntegralSource(Object t) {
        loginDAO.save(t);
    }
    //查看今天是否签到
    public boolean getCheckIn(int id,String time){
        String sql="select id from checkin where uid="+id+" and time='"+time+"' limit 1 ";
        return  loginDAO.getCheckIn(sql);
    }
    //判断是否为互为好友
    public boolean judgeFriend(int id,int id2){
        String sql = "select id from myfriends where uid=" + id + " and fid=" + id2 + " limit 1 ";
        return loginDAO.getId(sql) != 0;
    }
}
